package cn.ges.string;

import org.slf4j.helpers.MessageFormatter;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String 工具类
 *
 * @author gespent@163.com
 * @date 2018/12/3
 */
public class StringUtil {

    public static String toLowerCaseFirstOne(String s) {
        if (Character.isLowerCase(s.charAt(0))) {
            return s;
        } else {
            return Character.toLowerCase(s.charAt(0)) + s.substring(1);
        }
    }

    public static boolean isNum(String str) {
        if (StringUtil.isBlank(str)) {
            return false;
        } else {
            int sz = str.length();

            for (int i = 0; i < sz; ++i) {
                if (!Character.isDigit(str.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    }

    public static boolean notNum(String str) {
        return !StringUtil.isNum(str);
    }

    /**
     * 按顺序将args 塞入字符串中
     *
     * @param string 需要塞入参数的字符串 需要塞入参数的位置使用{}
     * @param args   需要格式化的参数
     * @return 拼接完毕的字符串
     */
    public static String format(String string, Object... args) {
        return MessageFormatter.arrayFormat(string, args).getMessage();
    }

    /**
     * 按角标顺序顺序将args 塞入字符串中
     *
     * @param string 需要塞入参数的字符串 需要塞入参数的位置使用{index}
     * @param args   需要塞入的参数
     * @return 拼接完毕的字符串
     */
    public static String formatByIndex(String string, Object... args) {
        return MessageFormat.format(string, args);
    }

    /**
     * 根据Unicode编码判断中文汉字和符号
     */
    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION;
    }

    public static boolean useChinese(String str) {
        final char[] chars = str.toCharArray();
        for (char aChar : chars) {
            if (isChinese(aChar)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isBigDecimal(String... arg) {
        try {
            for (String s : arg) {
                if (StringUtil.isBlank(s)) {
                    return false;
                }
                new BigDecimal(s);
            }

        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 替换 emoji表情 \xF0\x9F\x8D\x92\xE8\x96 [warning:过滤不完全]
     */
    public static String replaceEmoji(String str) {
        String patternString = "[\ud800\udc00-\udbff\udfff\ud800-\udfff]|[\uD83C\uDC04-\uD83C\uDE1A]|[\uD83D\uDC66-\uD83D\uDC69]|[\uD83D\uDC66\uD83C\uDFFB-\uD83D\uDC69\uD83C\uDFFF]|[\uD83D\uDE45\uD83C\uDFFB-\uD83D\uDE4F\uD83C\uDFFF]|[\uD83C\uDC00-\uD83D\uDFFF]|[\uD83E\uDD10-\uD83E\uDDC0]|[\uD83D\uDE00-\uD83D\uDE4F]|[\uD83D\uDE80-\uD83D\uDEF6]";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(str);
        return matcher.replaceAll("");
    }

    /**
     * 根据fileName返回文件后缀
     *
     * @param fileName 文件名称
     * @return 如果不能截取到后缀 返回null
     */
    public static String getSuffix(String fileName) {
        String substring;
        try {
            substring = fileName.substring(fileName.lastIndexOf("."));
        } catch (Exception e) {
            return null;
        }
        return substring;
    }

    /**
     * 检查字符串是否为空 长度是否在限定范围内
     */
    public static boolean lengthCheck(String str, int maxSize) {
        return StringUtil.isBlank(str) || str.length() > maxSize;
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str != null && (strLen = str.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotBlank(String str) {
        return !StringUtil.isBlank(str);
    }

    public static boolean theyHasBlank(String... strings) {
        for (String string : strings) {
            if (StringUtil.isBlank(string)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 手机号脱敏处理 中间四位替换为****
     *
     * @return 返回脱敏后手机号 如果传入手机号为空或不为11位 返回null
     */
    public static String phoneProcessing(String phoneNumber) {
        if (StringUtil.isBlank(phoneNumber) || phoneNumber.length() != 11) {
            return null;
        }
        String previous = phoneNumber.substring(0, 3);
        String follow = phoneNumber.substring(7, 11);
        phoneNumber = previous + "****" + follow;
        return phoneNumber;
    }
}
